package com.buybuyyi.stylistcrm.user.services;

import com.buybuyyi.stylistcrm.user.dao.UserProfileDao;
import com.buybuyyi.stylistcrm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import stylistcrm.core.services.order.enums.EnumExceptionCode;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.security.dto.UserDto;
import stylistcrm.core.services.security.exception.ReloginException;
import stylistcrm.core.services.security.exception.UserNameAlreadyExistsException;
import stylistcrm.core.services.security.service.SecurityService;
import stylistcrm.core.services.user.User;
import stylistcrm.core.services.user.profile.DesignerUserProfile;
import stylistcrm.core.services.user.profile.StylistUserProfile;
import stylistcrm.core.services.user.service.UserService;

/**
 * Created by yanwc on 2016/10/9.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    SecurityService securityService;

    /**
     * 用户管理数据访问层
     */
    @Autowired
    UserProfileDao userProfileDao;

    @Override
    public User getCurrentUser() throws BizException {
        //getCurrentUser实现
        try {
            UserDto userDto = securityService.getCurrentUser();
            User user = new User();
            if(null==userDto){
                throw new BizException(EnumExceptionCode.EC0005);
            }
            user.setLoginName(userDto.getLoginName());
            user.setUserName(userDto.getUserName());
            user.setPassword(userDto.getPassword());
            user.setId(userDto.getId());
            return user;
        } catch (ReloginException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User getUser(long secUserId) {
        UserDto userDto = securityService.getUserById(secUserId);
        User user = new User();
        user.setLoginName(userDto.getLoginName());
        user.setUserName(userDto.getUserName());
        user.setPassword(userDto.getPassword());
        user.setId(userDto.getId());
        //实现用户获取
        return user;
    }

    /**
     * 注册通用账号
     * @param userName
     * @param loginName
     * @param password
     * @return
     * @throws BizException
     */
    @Override
    public UserDto registerAccount(String userName,String loginName,String password) throws BizException {
        if(StringUtil.isBlank(userName)||StringUtil.isBlank(loginName)||StringUtil.isBlank(password)){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        try {
            UserDto account=securityService.singup(userName,loginName,password);
            return account;
        } catch (UserNameAlreadyExistsException e) {
            throw new BizException(EnumExceptionCode.EC0003);
        }
    }

    /**
     * 申请注册设计师
     * @param designer
     * @return
     * @throws BizException
     */
    @Override
    public Boolean registerDesigner(DesignerUserProfile designer) throws BizException {
        if(null==designer){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        return userProfileDao.registerDesigner(designer);
    }

    /**
     * 申请注册搭配师
     * @param stylist
     * @return
     * @throws BizException
     */
    @Override
    public Boolean registerStylist(StylistUserProfile stylist) throws BizException {
        if(null==stylist){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        return userProfileDao.registerStylist(stylist);
    }

}
